package cn.com.shaom.learning.sb.web.config;

import cn.com.shaom.learning.sb.web.CustomFilter;
import cn.com.shaom.learning.sb.web.CustomListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.Filter;

/**
 * ClassName: CustomMvcConfig
 * Description: 自定义的Mvc 配置，扩展SpringBoot的功能
 * Date: 2017/12/22 14:06 【需求编号】
 *
 * @author Shaom
 * @version V1.0.0
 */
@Configuration
public class CustomMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * 这边的配置不会覆盖SpringBoot的配置
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    /**
     * 自定义的 Filter,直接注册为 Bean 即可生效
     *
     * @return
     */
    @Bean
    public Filter filter() {
        return new CustomFilter();
    }

    /**
     * 自定义的 Listener。使用 RegistrationBean实现
     *
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean<CustomListener> listener() {
        return new ServletListenerRegistrationBean<>(new CustomListener());
    }
}
